<template>
  <div class="swiper-container" ref="cur">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(item, i) in skuInfo" :key="item.id" @click="getIndex(i)">
        <img :src="item.imgUrl" :class="{ active: activeIndex == i }" />
      </div>
    </div>
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
  </div>
</template>

<script>
import Swiper from 'swiper'
export default {
  name: 'ImageList',
  props: {
    skuInfo: {
      type: Array,
      default: () => {
        return []
      },
    },
  },
  data() {
    return {
      activeIndex: 0,
    }
  },
  watch: {
    // 数据是由父亲传过来的,理应是可以在mounted生命周期函数中直接使用的
    // watch侦听只能保证数据回来了
    skuInfo() {
      // nextTick能保证数据+结构完成
      this.$nextTick(() => {
        new Swiper(this.$refs.cur, {
          // 如果需要前进后退按钮
          navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
          },
          // 一页放多少个
          slidesPerView: 2,
        })
      })
    },
  },
  methods: {
    // 动态为高亮图片赋值
    getIndex(i) {
      // 当前组件用
      this.activeIndex = i
      // 传递给兄弟组件用
      this.$bus.$emit('getIndex', i)
    },
  },
}
</script>

<style lang="less" scoped>
.active {
  border: 2px solid #f60 !important;
  padding: 1px !important;
}
.swiper-container {
  height: 56px;
  // width: 412px;
  box-sizing: border-box;
  padding: 0 12px;

  .swiper-slide {
    width: 56px;
    height: 56px;

    img {
      width: 100%;
      height: 100%;
      border: 1px solid #ccc;
      padding: 2px;
      width: 50px;
      height: 50px;
      display: block;
    }
  }

  .swiper-button-next {
    left: auto;
    right: 0;
  }

  .swiper-button-prev {
    left: 0;
    right: auto;
  }

  .swiper-button-next,
  .swiper-button-prev {
    box-sizing: border-box;
    width: 12px;
    height: 56px;
    background: rgb(235, 235, 235);
    border: 1px solid rgb(204, 204, 204);
    top: 0;
    margin-top: 0;
    &::after {
      font-size: 12px;
    }
  }
}
</style>
